package com.spt.springbootzhsx.service;

import com.spt.springbootzhsx.entity.Movie;
import com.spt.springbootzhsx.entity.back.MovieBack;
import com.spt.springbootzhsx.entity.data.FloatData;
import com.spt.springbootzhsx.fileOp.ReadFile;
import com.spt.springbootzhsx.mapper.MovieMapper;
import com.spt.springbootzhsx.mapper.UserMapper;
import com.spt.springbootzhsx.redis.RedisOp;
import com.spt.springbootzhsx.service.suggest.Get_MTMC_mul_UWL_5.MU_Main;
import com.spt.springbootzhsx.service.suggest.Get_MovieToMovieCounts_3.GetMTMC_Main;
import com.spt.springbootzhsx.service.suggest.Get_MovieToMovie_2.GetMTM_Main;
import com.spt.springbootzhsx.service.suggest.Get_Result_7.Result_Main;
import com.spt.springbootzhsx.service.suggest.Get_Sum_StepFive_6.FiveSum_Main;
import com.spt.springbootzhsx.service.suggest.Get_UserToMovieList_1.GetUTM_Main;
import com.spt.springbootzhsx.service.suggest.Get_UserWatchList_4.GetMWL_Main;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

@Service
public class SuggestImpl implements SuggestService{
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisOp redisOp;

    @Autowired
    private ReadFile readFile;

    @Autowired
    private MovieMapper movieMapper;


    @Override
    public String doSuggest() {
        System.out.println("正在执行第一步");
        try {
            GetUTM_Main.domain();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("正在执行第二步");
        try {
            GetMTM_Main.domain();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("正在执行第三步");
        try {
            GetMTMC_Main.domain();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("正在执行第四步");
        try {
            GetMWL_Main.domain();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("正在执行第五步");
        try {
            MU_Main.domain();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("正在执行第六步");
        try {
            FiveSum_Main.domain();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("正在执行第七步");
        try {
            Result_Main.domain();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "协同过滤执行完成！";
    }

    @Override
    public String toRedis() {
        HashMap<String,String> map=new HashMap<>();
        try {
            String str=ReadFile.readFileToHdfs("src/main/java/com/spt/springbootzhsx/service/suggest/suggestOut/output/step07/part-r-00000");
            String[] strlist=str.split("\n");
            for(int i=0;i<strlist.length;i++){
                String[] k=strlist[i].split(",");
                if(map.containsKey("suggest"+k[0])){ //如果用户键值存在
                    String ss=map.get("suggest"+k[0]);
                    ss=ss+";"+k[1];
                    map.put("suggest"+k[0],ss);
                }else{  //如果用户键值不存在
                    map.put("suggest"+k[0],k[1]);
                }
            }
            Set<String> set=map.keySet(); //获取map键值的集合
            Iterator it =set.iterator();
            while (it.hasNext()){
                String key=it.next().toString();
                redisOp.set(key,map.get(key));
            }
            System.out.println("结果已经成功存入Redis");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "结果已经成功存入Redis!";
    }

    @Override
    public ArrayList<FloatData> getSuggestByUid(Integer uid) {
        ArrayList<FloatData> list=new ArrayList<>();
       String str=redisOp.get("suggest"+uid.toString());
       String[] suggests=str.split(";");
       for(int i=0;i<suggests.length;i++){
           String[] st=suggests[i].split("\t");
           Movie movie=movieMapper.getMovieByMid(Integer.valueOf(st[0]));
           FloatData floatData=new FloatData();
           floatData.setName(movie.getMname());
           floatData.setValue(Float.valueOf(st[1]));
           list.add(floatData);
       }
//       list.sort((o1, o2) -> {
//           float result = o1.getValue() - o2.getValue();
//           return result > 0 ? -1 : 1;
//       });
        Collections.sort(list);
       ArrayList<FloatData> list1=new ArrayList<>();
       int k=0;
        for (FloatData floatData : list) {
            list1.add(floatData);
            k++;
            if(k==80)break;
        }
       return list1;
    }

    @Override
    public ArrayList<MovieBack> getSuggestTop8(Integer uid) {
        ArrayList<MovieBack> list=new ArrayList<>();
        ArrayList<FloatData> list1=this.getSuggestByUid(uid);
        for(int i=0;i<list1.size()&&i<8;i++){
            Movie movie=movieMapper.getMovieByAllMname(list1.get(i).getName());
            MovieBack movieBack=new MovieBack();
            movieBack.setMid(movie.getMid());
            movieBack.setMrole(movie.getMrole());
            movieBack.setDirector(movie.getDirector());
            movieBack.setDesn(movie.getDesn());
            movieBack.setPic(movie.getPic());
            movieBack.setMtype(movie.getMtype());
            movieBack.setRate(movie.getRate());
            movieBack.setDuration(movie.getDuration());
            movieBack.setMlanguage(movie.getMlanguage());
            movieBack.setMname(movie.getMname());
            movieBack.setMstate(movie.getMstate());
            movieBack.setNickname(movie.getNickname());
            movieBack.setMcount(movie.getMcount());
            movieBack.setSuggest(list1.get(i).getValue());
            list.add(movieBack);
        }
        return list;
    }
}
